package com.sunlg.smarthome.contract.presenter;

import android.text.TextUtils;

import com.sunlg.smarthome.base.App;
import com.sunlg.smarthome.base.C;
import com.sunlg.smarthome.contract.GatewayContract;
import com.sunlg.smarthome.data.entity.Gateway;
import com.sunlg.smarthome.data.remote.MySubscriber;
import com.sunlg.smarthome.data.remote.Result;
import com.sunlg.smarthome.data.remote.SHServiceWrapper;
import com.sunlg.smarthome.utils.PreferHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sunlg on 2017/4/20.
 */

public class GatewayPresenter implements GatewayContract.Presenter {

    private GatewayContract.View mView;
    private SHServiceWrapper mSHServiceWrapper;
    private PreferHelper mPreferHelper;
    private PreferHelper mGatewayPreferHelper;

    public GatewayPresenter(){
        mSHServiceWrapper = App.getInstance().getServiceWrapper();
        mGatewayPreferHelper = new PreferHelper(App.getInstance(), "gateway");
        mPreferHelper = App.getInstance().getPreferHelper();
    }

    @Override
    public void init() {
/*        if(mGatewayPreferHelper.getBoolean("exist", false)){   //如果本地存在
            loadGateway(mGatewayPreferHelper.getInt("id", 0));
        }else {*/
            loadGateway();

    }

    @Override
    public void loadGateway(){
        //如果本地不存在则进行网络请求
        mSHServiceWrapper.getGatewayByUserId(mPreferHelper.getInt(C.PreKey.USER_ID, 0), new MySubscriber<Result<List<Gateway>>>() {
            @Override
            public void onSuccess(Result<List<Gateway>> data) {
                if(data.getCode() == C.ActionStatus.SUCCESS){
                    Gateway gateway = data.getData().get(0);
                    showView(gateway);
                    saveGate(gateway);   //保存至本地
                }else {
                    showView(null);
                }
            }
            @Override
            public void _onError(Throwable e) {
                mView.showError(e.getMessage());
            }
        });
    }

    @Override
    public void loadAllGateway() {
        mSHServiceWrapper.getGatewayByUserId(mPreferHelper.getInt(C.PreKey.USER_ID, 0), new MySubscriber<Result<List<Gateway>>>() {
            @Override
            public void onSuccess(Result<List<Gateway>> data) {
                if(data.getCode() == C.ActionStatus.SUCCESS){
                    mView.loadAllGatewaySuccess(data.getData());
                }else {
                    _onError(new Throwable(data.getMsg()));
                }
            }
            @Override
            public void _onError(Throwable e) {
                mView.showError(e.getMessage());
            }
        });
    }

    @Override
    public void loadGateway(Integer id) {
        mSHServiceWrapper.getGatewayById(id, new MySubscriber<Result<Gateway>>() {
            @Override
            public void onSuccess(Result<Gateway> data) {
                if(data.getCode() == C.ActionStatus.SUCCESS){
                    showView(data.getData());
                    saveGate(data.getData());
                }else {
                    showView(null);
                    _onError(new Throwable(data.getMsg()));
                }
            }

            @Override
            public void _onError(Throwable e){
                mView.showError(e.getMessage());
            }
        });
    }

    @Override
    public void switchGateway() {
        mSHServiceWrapper.getGatewayByUserId(mPreferHelper.getInt(C.PreKey.USER_ID, 0), new MySubscriber<Result<List<Gateway>>>() {
            @Override
            public void onSuccess(Result<List<Gateway>> data) {
                if(data.getCode() == C.ActionStatus.SUCCESS){
                    mView.showSwitchDialog(data.getData());

                }else {
                    mView.showError("没有可切换的网关");
                }
            }
            @Override
            public void _onError(Throwable e) {
                mView.showError(e.getMessage());
            }
        });
    }

    @Override
    public void addGateway(Gateway gateway) {
        Map<String, Object> params = new HashMap<>();
        params.put("no", gateway.getNo());
        String desc = gateway.getDescription();
        if(!TextUtils.isEmpty(desc)){
            params.put("description", desc);
        }

        mView.showBindProgress();
        mSHServiceWrapper.addGateway(params, new MySubscriber<Result>() {
            @Override
            public void onSuccess(Result data) {
                mView.hideBindProgress();
                if(data.getCode() == C.ActionStatus.SUCCESS){
                    mView.hideBindDialog();
                    loadGateway();
                }else {
                    _onError(new Throwable(data.getMsg()));
                }
            }

            @Override
            public void _onError(Throwable e) {
                mView.hideBindProgress();
                mView.showError(e.getMessage());
            }
        });
    }

    @Override
    public void deleteGateway() {
        mSHServiceWrapper.deleteGateway(mGatewayPreferHelper.getInt("id", 0), new MySubscriber<Result>() {
            @Override
            public void onSuccess(Result data) {
                if(data.getCode() == C.ActionStatus.SUCCESS){
                    showView(null);
                    mGatewayPreferHelper.clear();
                    mGatewayPreferHelper.setBoolean("exist", false);
                }else {
                    _onError(new Throwable(data.getMsg()));
                }
            }

            @Override
            public void _onError(Throwable e) {
                mView.showError(e.getMessage());
            }
        });
    }

    @Override
    public void updateGateway(Gateway gateway) {
        mSHServiceWrapper.updateGateway(gateway,
                new MySubscriber<Result>() {
                    @Override
                    public void onSuccess(Result data) {
                        if(data.getCode() == C.ActionStatus.SUCCESS){
                            mView.updateGatewaySuccess();
                        }else {
                            _onError(new Throwable(data.getMsg()));
                        }
                    }

                    @Override
                    public void _onError(Throwable e) {
                        mView.showError(e.getMessage());
                    }
                });
    }

    @Override
    public void attachView(GatewayContract.View view) {
        this.mView = view;
    }

    @Override
    public void dettachView() {
        this.mView = null;
    }

    /**
     * 保存gateway信息
     * @param gateway
     */
    private void saveGate(Gateway gateway){
        if(gateway != null){
            mGatewayPreferHelper.setBoolean("exist", true);
            mGatewayPreferHelper.setInt("id", gateway.getId());
            mGatewayPreferHelper.setInt("userId", gateway.getUserId());
            mGatewayPreferHelper.setInt("no", gateway.getNo());
            mGatewayPreferHelper.setBoolean("status", gateway.getStatus());
            if(!TextUtils.isEmpty(gateway.getIp())){
                mGatewayPreferHelper.setString("ip", gateway.getIp());
            }
            if(!TextUtils.isEmpty(gateway.getDescription())){
                mGatewayPreferHelper.setString("desc", gateway.getDescription());
            }
        }
    }

    private void showView(Gateway gateway){
        if(gateway != null){
            mView.hideFab();
            mView.hideNoneGatewayText();
            mView.showGatewayInfo(gateway);
            if(!gateway.getStatus()){  //网关离线
                mView.showOfflineImg();
            }else {  //网关在线
                mView.hideGatewayStatusImg();
            }
        }else {
            mView.showFab();
            mView.showNoneGatewayText();
            mView.showNoneGatewayImg();
            mView.hideGatewayInfo();
        }
    }
}
